package com.reading.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.asymmetric.Sign;
import com.reading.feign.UserFeignClient;
import com.reading.mapper.ShellMapper;
import com.reading.mapper.ShellRecordMapper;
import com.reading.mapper.SignInMapper;
import com.reading.pojo.GlodVO;
import com.reading.pojo.Shell;
import com.reading.pojo.ShellRecord;
import com.reading.pojo.SignIn;
import com.reading.pojo.user.User;
import com.reading.service.SignInService;
import com.reading.vo.SignVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SignInServiceImpl implements SignInService {

    @Autowired
    private SignInMapper signInMapper;

    @Autowired
    private ShellMapper shellMapper;

    @Autowired
    private ShellRecordMapper shellRecordMapper;

    @Autowired
    private UserFeignClient userFeignClient;
    @Override
    public Integer findContinuous(Integer userId) {
        //定义连续签到天数
        int day = 0;
        //获取今天的时间
        Date today = findToday();
        //查询该用户最后的签到数据
        SignIn one = signInMapper.findLastData(userId);
        //判断该用户是否有签到记录
        if (one != null){
            //获取最后签到的时间
            Date date = one.getCreateTime();
            String yesterdayStr = DateUtil.formatDate(date);
            Date createTime = DateUtil.parse(yesterdayStr);
            //判断今天的时间和最后签到的时间是否相等
            boolean isEquals = DateUtil.isSameDay(today, createTime);
            //判断最后签到数据中的连续签到天数是否为7，并且今天和最后签到的时间不相等
            if (one.getContinuousDays().equals(7 + "") & isEquals==false){
                //签到天数置为0
                return day;
            }else{
                //获取时间差，如果今天的时间和用户最后签到时间差值大于1天，则签到天数置为0
                long between = DateUtil.between(createTime, today, DateUnit.DAY);
                if (between >1){
                    return day;
                }else{
                    //不大于1，则获取用户最后签到数据的连续签到天数
                    int i = Integer.parseInt(one.getContinuousDays());
                    return i;
                }
            }
        }else{
            //没有则连续签到天数为0
            return day;
        }
    }

    @Override
    public SignIn findPrevious(SignVo signVo) {
        Date date = findYesterday();
        signVo.setCreateTime(date);
        signVo.setUserId(signVo.getUserId());

        return signInMapper.findPrevious(signVo);
    }

    @Override
    public SignIn findLastData(Integer userId) {
        return signInMapper.findLastData(userId);
    }

    @Override
    public String toSignIn(Integer userId) {
        //获取该用户最后的连续签到天数
        int day = findContinuous(userId);
        //根据连续签到天数获取对应的贝壳数
        Shell shell = shellMapper.findById(day+1);
        //用户签到
        //添加签到记录
        SignIn signIn = new SignIn();
        signIn.setUserId(userId);
        signIn.setCreateTime(new Date());
        signIn.setContinuousDays(day+1+"");
        signInMapper.insert(signIn);
        //添加贝壳记录
        ShellRecord shellRecord = new ShellRecord();
        shellRecord.setUserId(userId);
        shellRecord.setRecord(shell.getRecord());
        shellRecord.setNumber(shell.getShellNumber());
        shellRecord.setCreateTime(new Date());
        shellRecordMapper.save(shellRecord);
        //修改对应用户的贝壳数量
        User user = userFeignClient.findOne(userId);
        GlodVO glodVO = new GlodVO();
        glodVO.setGlod2(user.getUserglod2()+shell.getShellNumber());
        glodVO.setId(user.getId());
        userFeignClient.updateUserGlod2(glodVO);
        return null;
    }

    @Override
    public SignIn findTodayData(SignVo signVo) {
        Date today = findToday();
        signVo.setCreateTime(today);
        return signInMapper.findTodayData(signVo);
    }

    public Date findYesterday(){
        DateTime yesterday = DateUtil.yesterday();
        String yesterdayStr = DateUtil.formatDate(yesterday);
        Date date = DateUtil.parse(yesterdayStr);
        return date;
    }

    public Date findToday(){
        String today = DateUtil.today();
        Date date = DateUtil.parse(today);
        return date;
    }
}
